package com.wangchao.administrator.zenmechi.ui.fragment.shopfragment;

import android.content.Context;
import android.util.Log;

import com.wangchao.administrator.zenmechi.bean.categorybean.CategoryBean;
import com.wangchao.administrator.zenmechi.bean.categorybean.CategoryDataBean;
import com.wangchao.administrator.zenmechi.bean.shopcategorybean.ShopCategoryBean;
import com.wangchao.administrator.zenmechi.bean.shopcategorybean.ShopCategoryResultBean;
import com.wangchao.administrator.zenmechi.config.UrlConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;

/**
 * Created by Administrator on 2016/10/19.
 */
public class ShopFragmentPresenter implements ShopFragmentContract.Presenter {

    private ShopFragmentContract.View view;
    private ShopFragmentContract.Model model;
    private Map<String,String> map = new HashMap<>();
    private List<ShopCategoryResultBean> resultBeens = new ArrayList<>();
    private MyAdapterForShopExpand adapter;
    private Map<String,String> map02 = new HashMap<>();
    private List<CategoryDataBean> dataBeens = new ArrayList<>();
    private MyAdapterForShowList adapter02;


    public ShopFragmentPresenter(ShopFragmentContract.View view) {
        this.view = view;
        this.model = new ShopFragmentModel();
    }


    @Override
    public void getCategoryData(final Context context) {
        map.put(UrlConfig.Params.MARKET_ID,UrlConfig.DefaultVaule.MARKET_ID+"");
        model.getCategoryData(map, new Subscriber<ShopCategoryBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ShopCategoryBean shopCategoryBean) {
                resultBeens = shopCategoryBean.getResult();
                //Log.i("tag","----------"+resultBeens.size());
                adapter = new MyAdapterForShopExpand(context,resultBeens);
                view.initExpandListView(adapter);
            }
        });
    }

    public String getCateId(int i){
        return resultBeens.get(i).getId()+"";
    }
    public String getCateName(int i){
        return resultBeens.get(i).getName();
    }

    public String getCateChildsId(int i,int i1){
        return resultBeens.get(i).getChilds().get(i1).getId()+"";
    }
    public String getCateChildsName(int i,int i1){
        return resultBeens.get(i).getChilds().get(i1).getName();
    }

    @Override
    public void getCateList(final Context context, String id) {
        map02.put(UrlConfig.Params.ACCESS_TOKEN,UrlConfig.DefaultVaule.ACCESS_TOKEN);
        map02.put(UrlConfig.Params.PAGE_SIZE,5+"");
        map02.put(UrlConfig.Params.PAGE,UrlConfig.DefaultVaule.PAGE_NUM+"");
        map02.put(UrlConfig.Params.MARKET_ID,UrlConfig.DefaultVaule.MARKET_ID+"");
        map02.put(UrlConfig.Params.SALE_SORT,UrlConfig.DefaultVaule.SALE_SORT);
        map02.put(UrlConfig.Params.CATEGORY_ID,id);
        model.getCateList(map02, new Subscriber<CategoryBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.i("tag","----------"+e.toString());
            }

            @Override
            public void onNext(CategoryBean categoryBean) {
                dataBeens =categoryBean.getResult().getData();
                if (dataBeens!=null&&dataBeens.size()!=0){
                    view.hideLayout();
                    //Log.i("tag","----------"+dataBeens.size());
                    adapter02 = new MyAdapterForShowList(context,dataBeens);
                    view.showList(adapter02);
                }else {
                    view.showLayout();
                }
            }
        });
    }
}
